
#include "msg_fes.h"
#include "client_mgr.h"
#include "frontend_service.h"

#include <server_share/msg_struct_login.h>
#include <server_share/server_def.h>

using namespace NLNET;
using namespace FES;
using namespace DEF;

extern CCallbackServer *Clients;
//extern std::string            inetIP;
//extern uint16                 inetPort;
//extern NLNET::CMessage        msgout;

//void cbWSConnection (const std::string &serviceName, NLNET::TServiceId  sid, void *arg)
//{
//	CMessage        msgout;
//	msgout.setType("FS_STATE");
//	msgout.serial(inetIP);
//	msgout.serial(inetPort);
//	NLNET::CUnifiedNetwork::getInstance()->send (sid, msgout);
//}

//void cbAcceptClients( NLNET::CMessage& msgin, const std::string &serviceName, NLNET::TServiceId serviceId )
//{
//	nlinfo("cbAcceptClients: asked by service %s %d to accept clients (patching is no longer available on this server)", serviceName.c_str(), serviceId.get());
//	CFrontEndService::instance()->AcceptClients = true;
//}

void cbLSConnection (const std::string &serviceName, TServiceId  sid, void *arg)
{
	
	MSG_STR::CFrontEnd frontEnd;
	frontEnd.SId   = TServiceId(FrontEndService->ConfigFile.getVar ("SId").asInt());
	frontEnd.Ip    = FrontEndService->ConfigFile.getVar ("INetIP").asString();
	frontEnd.Port  = FrontEndService->ConfigFile.getVar ("INetPort").asInt();
	frontEnd.MaxPlayers  = FrontEndService->ConfigFile.getVar ("INetPort").asInt();
	frontEnd.CurrPlayers = ClientMgr.currClients();

	CMessage        msgout("SET_FES_INFO");
	msgout.serial(frontEnd);
	NLNET::CUnifiedNetwork::getInstance()->send (sid, msgout);
}

void cbUserInfo ( NLNET::CMessage& msgin, const std::string &serviceName, NLNET::TServiceId serviceId )
{
	DEF::UID id;
	msgin.serial(id);
	CClient* pClient = ClientMgr.findClient(id);

	if(pClient==NULL)	{return;}

	msgin.invert();
	Clients->send( msgin, pClient->con );
}

void cbErr ( NLNET::CMessage& msgin, const std::string &serviceName, NLNET::TServiceId serviceId )
{
	ERROR_TYPE errNO;
	UID        uid;
	msgin.serial(errNO);
	msgin.serial(uid);

	switch (errNO)
	{
	case PLAYER_ONLINE_TO_FES:
		
		break;
	case SERVER_FULL:
		{
			CClient* pClient = ClientMgr.findClient(uid);
			if ( pClient != NULL )
			{
				sendERR( SERVER_FULL, pClient->con, Clients );
				ClientMgr.removeClient(pClient->uid);
			}
			break;
		}
	default:
		break;
	}
}








